clear all;
close all;
clc

figure;
xlim([-4 4]);
hold on;
for i=-4:1:4
%     x=i-1:0.001:i+2;
    x=-5:0.001:6;
    y=zeros(1,11001);
    c=0;
%     if i-1<=x & x<=i
        for j=(i-1+5)*1000+1:(i+5)*1000+1
            x(j)=i-1+0.001*c;
            c=c+1;
            y(j)=1/2*(x(j)-i+1)*(x(j)-i+1);
        end
%     else if i<=x & x<=i+1
            for j=(i+5)*1000+2:(i+6)*1000+1
                x(j)=i-1+0.001*c;
                c=c+1;
                y(j)=1/2*(-2*x(j)*x(j)+(4*i+2)*x(j)-2*i*i-2*i+1);
            end
%         else
            for j=(i+6)*1000+2:(i+7)*1000+1
                x(j)=i-1+0.001*c;
                c=c+1;
               y(j)=1/2*(i+2-x(j))*(i+2-x(j));
            end
%         end
%     end
    plot(x,y,'Linewidth',2);
    end
    
% fff=fplot('cc',[-1,1],'Linewidth',2);
% legendText = legend([l(1),l(4),l(7),'n=5','n=10','n=15','n=20','f(x)','FontSize', 18,'FontName','problem_E 2','location','northeast');